#include <string.h>
#include <jni.h>
#include <android/log.h>

#include "HoneywellXML.h"
#include "HoneywellUDP.h"
extern HRIS_1000_SYSTEM	hris1000_system;


extern int send_cmd_light(int id, int action, int onoff, int dimmer);
extern int send_cmd_ac485_cfg(int id, int onoff, int mode, int fan, int dir, int temp);
extern int send_cmd_ac485_req(int id, int *onoff, int *mode, int *fan,
					   int *dir, int *temp_set, int *temp_cur);
extern int send_cmd_irac(int id, int irid);
extern int send_cmd_irdev(int, int);
extern int send_cmd_ufh_cfg(int id, int onoff, int temp_set);
extern int send_cmd_ufh_req(int id, int *onoff, int *temp_set, int *temp_cur);
extern int send_cmd_relay_cfg(int id, int onoff);
extern int send_cmd_relay_req(int id, int *onoff);
extern int send_cmd_curtain(int id, int action, int onoff);
extern int send_cmd_zone_cfg(int zoneid, int ztype, int action);
extern int send_cmd_zone_req(int zoneid, int ztype, int statuskey, int *nstatuskey, char *zoneinfo);
extern int send_cmd_scenario_cfg(int sid);
extern int send_cmd_scenario_req(int areaid, int *sid);

extern char mobotSn[64];
extern void get_dpf_sn(char *mac_str);
extern int authProc(char *mac, char *authStr);

extern char hris1000_ip[32];
extern char gUserName[64];
extern char gPassword[64];
extern char UDID[128];
extern char authString[64];
extern int auth_flag;

char macStr[64];

jint
Java_com_android_dfsc_SmartHomeEngine_initEngine( JNIEnv* env,
                                                  jobject thiz )
{
	  __android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in initEngine");
	  //get_system_config("./HoneywellSystem.xml");
	  //FetchXMLFile(); 
	  FetchXMLData(); 
	  init_UDP_sock(hris1000_ip, 9899);
	  
    return 1;
}

jint
Java_com_android_dfsc_SmartHomeEngine_syncData( JNIEnv* env,
                                                  jobject thiz )
{
	  __android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in syncData");
	  int ret = FetchXMLFile();
	  __android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in FetchXMLFile return value is: %d",ret);
	  if ( ret != 0)  
	  	return -1;
	  __android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in syncData FetchXMLData"); 
	  if ( FetchXMLData() == -1 )
	  	return -1; 
		
    return 0;
}

void 
Java_com_android_dfsc_SmartHomeEngine_destoryEngine( JNIEnv* env,
                                                  jobject thiz )
{
		__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in destoryEngine");
		close_UDP_sock();
    return ;
}


jint Java_com_android_dfsc_SmartHomeEngine_sendCmdLight(JNIEnv* env,
        jobject thiz,jint id, jint action, jint onoff, jint dimmer)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdLight");
	return send_cmd_light(id, action, onoff, dimmer);
	
}
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdAc485Cfg(JNIEnv* env,
        jobject thiz,jint id, jint onoff, jint mode, jint fan, jint dir, jint temp)
{
	return 0;
}
jobject Java_com_android_dfsc_SmartHomeEngine_sendCmdAc485Req(JNIEnv* env,
        jobject thiz,jint id)
{
	return 0;
}
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdIrac(JNIEnv* env,
        jobject thiz,jint id, jint irid)
{
	return 0;
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdIrdev(JNIEnv* env,
        jobject thiz,jint id, jint irid)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdIrdev");
	return send_cmd_irdev(id, irid);
};

jint Java_com_android_dfsc_SmartHomeEngine_sendCmdUfhCfg(JNIEnv* env,
        jobject thiz,jint id, jint onoff, jint tempSet)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdUfhCfg");
	return send_cmd_ufh_cfg(id,  onoff, tempSet);
	
};
jobject Java_com_android_dfsc_SmartHomeEngine_sendCmdUfhReq(JNIEnv* env,
        jobject thiz,jint id)
{
	return NULL;
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdRelayCfg(JNIEnv* env,
        jobject thiz,jint id, jint onoff)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdRelayCfg");
	send_cmd_relay_cfg( id,  onoff);
	return send_cmd_relay_cfg(id,  onoff);
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdRelayReq(JNIEnv* env,
        jobject thiz,jint id)
{
	return 0;
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdCurtain(JNIEnv* env,
        jobject thiz,jint id, jint action, jint onoff)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdCurtain");
	return send_cmd_curtain( id,  action,  onoff);

};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdZoneCfg(JNIEnv* env,
        jobject thiz,jint zoneid, jint ztype, jint action)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdZoneCfg");
	return send_cmd_zone_cfg( zoneid, ztype,  action);
};
jobject Java_com_android_dfsc_SmartHomeEngine_sendCmdZoneReq(JNIEnv* env,
        jobject thiz,jint zoneid, jint ztype, jint statuskey)
{
	return NULL;
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdScenarioCfg(JNIEnv* env,
        jobject thiz,jint sid)
{
	__android_log_write(ANDROID_LOG_DEBUG, "honey-TEST", "in sendCmdScenarioCfg");
	return send_cmd_scenario_cfg(sid);
};
jint Java_com_android_dfsc_SmartHomeEngine_sendCmdScenarioReq(JNIEnv* env,
        jobject thiz,jint areaid)
{
	return 0;
};
	
jobject Java_com_android_dfsc_SmartHomeEngine_getAppData(JNIEnv* env,
        jobject thiz)
{
	jclass hris1000SystemClass = (*env)->FindClass(env,
			"com/android/dfsc/data/Hris1000System");
	if (hris1000SystemClass == NULL)
		return NULL;
	jobject result = (*env)->AllocObject(env, hris1000SystemClass);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, hris1000SystemClass);
		return NULL;
	}
	jfieldID fid;
	fid = (*env)->GetFieldID(env, hris1000SystemClass, "isReady", "I");
	(*env)->SetIntField(env, result, fid, hris1000_system.is_ready);
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in getAppData 999");
	fid = (*env)->GetFieldID(env, hris1000SystemClass, "version", "I");
	
	(*env)->SetIntField(env, result, fid, hris1000_system.version);
	(*env)->DeleteLocalRef(env, hris1000SystemClass);
	return result;
}


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getZoneData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.zone_info.zone_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "zone length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_ZONE * pdata=hris1000_system.zone_info.zone;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getAreaData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.area_info.area_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "area length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_AREA * pdata=hris1000_system.area_info.area;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			//SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getWirelessZoneData(JNIEnv* env,
        jobject thiz)
{
		int length=hris1000_system.wireless_zone_info.wizone_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "wireless zone length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_WIZONE * pdata=hris1000_system.wireless_zone_info.wizone;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getScenarioData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.scenario_info.scenario_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "scenario length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisScenarioInfo");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_SCENARIO * pdata=hris1000_system.scenario_info.scenario;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			//SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			SetCommdataIntField( env,class, element,"isEnergy",pdata->is_energy);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};
jobjectArray Java_com_android_dfsc_SmartHomeEngine_getFastScenarioData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.fast_scenario_info.fast_scenario_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "fast scenario length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisFastScenarioInfo");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_SCENARIO * pdata=hris1000_system.fast_scenario_info.fast_scenario;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			//SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			//SetCommdataIntField( env,class, element,"isEnergy",pdata->is_energy);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};

jobjectArray Java_com_android_dfsc_SmartHomeEngine_getLightData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.device_info.light_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_LIGHT * pdata=hris1000_system.device_info.light;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getAc485Data(JNIEnv* env,
        jobject thiz)
{
	return NULL;
};

jobjectArray Java_com_android_dfsc_SmartHomeEngine_getCurtainData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.device_info.curtain_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "curtain length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_CURTAIN * pdata=hris1000_system.device_info.curtain;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			jstring name=(*env)->NewStringUTF(env, pdata->name);
			SetCommdataStringField( env,class, element,"name",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};
jobjectArray Java_com_android_dfsc_SmartHomeEngine_getUFHData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.device_info.UFH_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "UFH length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_UFH * pdata=hris1000_system.device_info.UFH;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			//jstring name=(*env)->NewStringUTF(env, pdata->name);
			//SetCommdataStringField( env,class, element,"name",name);
			//(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};

jobjectArray Java_com_android_dfsc_SmartHomeEngine_getRelayData(JNIEnv* env,
        jobject thiz)
{
	int length=hris1000_system.device_info.relay_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "relay length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisCommObject");
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_RELAY * pdata=hris1000_system.device_info.relay;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			//jstring name=(*env)->NewStringUTF(env, pdata->name);
			//SetCommdataStringField( env,class, element,"name",name);
			//(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getIracData(JNIEnv* env,
        jobject thiz)
{
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in getIracData");
	int length=hris1000_system.device_info.irac_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "irac length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisIrdev");
	
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_IRAC * pdata=hris1000_system.device_info.irac;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			SetCommdataIntField( env,class, element,"modeNum",pdata->mode_num);
			
			
			//jstring name=(*env)->NewStringUTF(env, pdata->name);
			//SetCommdataStringField( env,class, element,"name",name);
			//(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 	
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jobjectArray Java_com_android_dfsc_SmartHomeEngine_getIracModeData(JNIEnv* env,
        jobject thiz,int index)
{
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in getIracModeData");
	if ( index >= hris1000_system.device_info.irac_num )
		return NULL;
	int i;
	//get current irdev
	HRIS_IRAC * pdata=hris1000_system.device_info.irac;
	
	while (i< index)  
	{
		pdata=pdata->next;
		i++;
	}
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisIrdevMode");
	if (class == NULL)
	{
		return NULL;
	}
	int length=pdata->mode_num;
	jobjectArray result = (*env)->NewObjectArray(env,length , class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"modeId",pdata->mode[i].modeid);
			jstring name=(*env)->NewStringUTF(env, pdata->mode[i].modename);
			SetCommdataStringField( env,class, element,"modeName",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};

jobjectArray Java_com_android_dfsc_SmartHomeEngine_getIrdevData(JNIEnv* env,
        jobject thiz)
{
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in getIrdevData");
	int length=hris1000_system.device_info.irdev_num;
	int i;
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "irdev length is : %d",length);
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisIrdev");
	
	if (class == NULL)
	{
		return NULL;
	}
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	HRIS_IRDEV * pdata=hris1000_system.device_info.irdev;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"id",pdata->id);
			SetCommdataIntField( env,class, element,"areaId",pdata->area_id);
			SetCommdataIntField( env,class, element,"modeNum",pdata->mode_num);
			
			
			//jstring name=(*env)->NewStringUTF(env, pdata->name);
			//SetCommdataStringField( env,class, element,"name",name);
			//(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 	
		 }
		pdata=pdata->next;	 
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};

//public native HrisIrdevMode[]	getIrdevModeData(int i);
jobjectArray Java_com_android_dfsc_SmartHomeEngine_getIrdevModeData(JNIEnv* env,
        jobject thiz,int index)
{
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in getIrdevModeData");
	if ( index >= hris1000_system.device_info.irdev_num )
		return NULL;
	int i;
	//get current irdev
	HRIS_IRDEV * pdata=hris1000_system.device_info.irdev;
	while (i< index)
	{
		pdata=pdata->next;
		i++;
	}
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisIrdevMode");
	if (class == NULL)
	{
		return NULL;
	}
	int length=pdata->mode_num;
	jobjectArray result = (*env)->NewObjectArray(env,length , class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	//HRIS_IRDEV * pdata=hris1000_system.device_info.irdev;
	for (i=0;i<length;i++)
	{
		//__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "light sequence is : %d",i);
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
			SetCommdataIntField( env,class, element,"modeId",pdata->mode[i].modeid);
			jstring name=(*env)->NewStringUTF(env, pdata->mode[i].modename);
			SetCommdataStringField( env,class, element,"modeName",name);
			(*env)->DeleteLocalRef(env, name);
		 	(*env)->DeleteLocalRef(env, element);
		 }
	}
	
	(*env)->DeleteLocalRef(env, class);
	return result;
};


jstring Java_com_android_dfsc_SmartHomeEngine_getAuthCode(JNIEnv* env,
        jobject thiz ,jobject app)
{
	 
	 jstring     authCode = NULL;
	 
	 jmethodID mid;
	  jclass class = (*env)->GetObjectClass(env, app);
	  mid = (*env)->GetMethodID(env, class, "getMac", "()Ljava/lang/String;");
	  if (mid == NULL)
	  	return 1;
	  jstring mac = (*env)->CallObjectMethod(env, app, mid);
	  
	  const jchar * authStr = (*env)->GetStringUTFChars(env, mac, NULL);
	  
	  strcpy(macStr,authStr);
	  //strcpy(macStr, (*env)->GetStringUTFChars(env, mac, NULL));
	  __android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "mac in c value is: %s",authStr);
	  
	 //const jchar * authStr = (*env)->GetStringUTFChars(env, authString, NULL);
	 //(char *) authStr;
	 //__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authString : %s",authString);
	 //__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authStr : %s",(char *)authStr);
	 get_dpf_sn((char *) authStr);
	 //__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authStrxxx : ");
	 //__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authStrxxx :%s ",mobotSn);
	 authCode = (*env)->NewStringUTF(env, (char *)mobotSn);
	 __android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authcode mac in c value is: %s",(char *)authStr);
	 __android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authcode : %s",mobotSn);
	 return authCode;

};


void Java_com_android_dfsc_SmartHomeEngine_syncConfig(JNIEnv* env,
        jobject thiz,jstring ip,jstring userName,jstring password,jstring UDID,jstring authCode)
{
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "in syncConfig ");
	const jchar * ipStr = (*env)->GetStringUTFChars(env, ip, NULL);
	const jchar * userNameStr = (*env)->GetStringUTFChars(env, userName, NULL);
	const jchar * passwordStr = (*env)->GetStringUTFChars(env, password, NULL);
	const jchar * UDIDStr = (*env)->GetStringUTFChars(env, UDID, NULL);
	const jchar * authCodeStr = (*env)->GetStringUTFChars(env, authCode, NULL);
	//set C variable
	auth_flag = 0;
	
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authProc mac in c value is: %s",macStr);
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "authProc authCodeStr in c value is: %s",authCodeStr);
	auth_flag = authProc(macStr, authCodeStr);
	
	/*
	if(strcmp(authCodeStr, "89ab4c5599635b0a") == 0
		&& strcmp(UDIDStr, "051BB9696EA0BB") ==0 )
		auth_flag = 1;
	else	auth_flag = 0;
	//auth_flag = 1;*/
	__android_log_print(ANDROID_LOG_DEBUG, "honey-TEST", "auth_flag:%d ",auth_flag);
	
	
	if(ipStr != NULL)
		strcpy(hris1000_ip, ipStr);
	else strcpy(hris1000_ip, "192.168.0.40");
	if(userNameStr != NULL)
		strcpy(gUserName, userNameStr);
	else strcpy(gUserName, "admin");
	if(gPassword != NULL)
		strcpy(gPassword, passwordStr);
	else strcpy(gPassword, "admin");
	//strcpy(UDID, UDIDStr);
	//strcpy(authString, authCodeStr);
	return;
}

void SetCommdataIntField(JNIEnv* env,jclass class,jobject element,char * attrName,int value)
{
	jfieldID fid;
	fid = (*env)->GetFieldID(env, class, attrName, "I");
	(*env)->SetIntField(env, element, fid, value);
	return;
};

void SetCommdataStringField(JNIEnv* env,jclass class,jobject element,char * attrName,jstring value)
{
	jfieldID fid;
	fid = (*env)->GetFieldID(env, class, attrName, "Ljava/lang/String;");
	(*env)->SetObjectField(env, element, fid, value);
	return;
};





//NOT IN USING
jobjectArray Java_com_android_dfsc_SmartHomeEngine_getDeviceData(JNIEnv* env,
        jobject thiz)
{
	int length=4;
	int i;
	jclass class = (*env)->FindClass(env,
			"com/android/dfsc/data/HrisDeviceInfo");
	if (class == NULL)
		return NULL;
	jobjectArray result = (*env)->NewObjectArray(env, length, class,NULL);
	if (result == NULL)
	{
		(*env)->DeleteLocalRef(env, class);
		return NULL;
	}
	for (i=0;i<length;i++)
	{
		jobject element = (*env)->AllocObject(env, class);
		(*env)->SetObjectArrayElement(env, result, i, element);
		if (element != NULL )
		{
		 	(*env)->DeleteLocalRef(env, element);
		 }
	}
	(*env)->DeleteLocalRef(env, class);
	return result;
};
